var pagePath = "";
var resourcePath = "";
$(function(){
//	$("input[type='text']").attr("maxlength","80");
//	$("textarea").attr("maxlength","1000");
	pagePath = $("#pagePath").val();
	resourcePath = $("#resourcePath").val();
	//初始化加载用户管理
	optTab(1);
});

function authorizeManager(){
	window.location.href=pagePath + "/settings/user/authorizeManager.html";
}
/**
 * 操作Tab的点击事件
 * @param type指定模块[1:用户管理,2:用户组管理,3,角色管理，4：角色组管理,5:功能菜单管理]
 * 对应模块操作optType[11:用户新增，12:用户修改]
 * 各个模块操作公共参数commType[999:多删,998:单删]
 */
function optTab(type){
	var html = "";
	var url = "";
	$(".set-ck").html("");
	if(1 == type){
		url = pagePath + "/settings/user/userList.json";
		html += "<div class='set-t0'>" +
		"<div class='se-bt clearFix'>" +
			"<div class='sel w140'><i class='d-name'>用户名称</i><input id='searchUsername' class='input-w140' type='text' placeholder='用户名称'></div>" +
				"<div class='sel w305'><i class='d-name'>公司</i>" +
					"<div class='dropdown-list'>" +
						"<div class='drop-list drop-list-min'>" +
							"<div class='checked w270' id='SCompDiv'><a href='javascript:void(0);' id='gs'>请选择公司</a></div><span class='down-span'></span>" +
						"</div>" +
						getCompanyHtml() +
					"</div>" +
				"</div>" +
				"<div class='sel w305'>" +
					"<i class='d-name'>部门</i>" +
					"<div class='dropdown-list'>" +
						"<div class='drop-list drop-list-min'>" +
							"<div class='checked w270' id='SDeptDiv'><a href='javascript:void(0);' id='bm'>请选择部门</a></div> <span class='down-span'></span>" +
						"</div>" +
						"<ul class='select select-min none' ></ul>" +
					"</div>" +
				"</div>"+
				"<div class='sel w305'>"+
					"<i class='d-name'>是否已授权</i>"+
					"<div class='dropdown-list'>"+
						"<div class='drop-list drop-list-min'>"+
							"<div class='checked w270' style='width:60px;' id='SIsAuthorizeDiv'>"+
								"<a href='javascript:void(0);' id='0'>未授权</a>"+
							"</div>"+
							" <span class='down-span'></span>"+
						"</div>"+
						"<ul class='select select-min none' style='width:85px;'>"+
							"<li><a href='javascript:void(0);' id='0'>未授权</a></li>"+
							"<li><a href='javascript:void(0);' id='1'>已授权</a></li>"+
						"</ul>"+
					"</div>" +
				"</div>"+					
				"<a href='javascript:void(0)' class='btn btn-w100 btn-search-icon btn-h30' style='float:left' onclick='searchFun(1,this);'>查询</a>"+
			"</div>"+
			"<p class='tool pad20' style='margin-top:0px' >" +
			"<a href='javascript:void(0);' data-toggle='modal' class='btn  btn-add-icon1 ' onclick='addOrModify(11,null);'><em></em>新建</a>" +
			"<a href='javascript:void(0);' class='btn btn-suo-icon ' onclick='authorizeMutiply();'><em></em>批量授权</a>" +
			"<a href='javascript:void(0);' class='btn btn-suo-icon ' onclick='downloadModel();'><em></em>模板下载</a>" +
			"<a href='javascript:void(0);' class='btn-del-icon' onclick='deleteData(1,999,null);'><em></em>删除</a>" +
			"<a href='javascript:void(0);' class='btn-dr-icon' onclick='importData();'><em></em>导入</a>" +
			"<a href='javascript:void(0);' class='btn-dc-icon' onclick='exportData(this);'><em></em>导出</a>" +
			"<a href='javascript:void(0);' class='btn-gb-icon' onclick='dataOnOrOff(1,999,0);'><em></em>禁用</a>" +
			"<a href='javascript:void(0);' class='btn-qy-icon' onclick='dataOnOrOff(1,999,1);'><em></em>启用</a>" +
			"<a href='javascript:void(0);' class='btn-suo-icon' onclick='resetPwd(999,null);'><em></em>重置密码</a>" +
			"</p>" +
		"</div>";
	}else if(2==type){
		url = pagePath+"/settings/userGroup/userGroupList.json";
		html += "<div class='set-t0'>" +
					"<div class='se-bt clearFix'>" +
						"<div class='sel w305'>" +
							"<i class='d-name'>用户组名称</i>" +
							"<input class='input-w295' type='text' placeholder='用户组名称'>" +
						"</div>" +
						"<div class='sel w140'>" +
							"<i class='d-name'>用户组编号</i>" +
							"<input class='input-w140' type='text' placeholder='用户组编号'>" +
						"</div>" +
						"<a href='javascript:void(0)' class='btn btn-w100 btn-search-icon btn-h30' style='float:left' onclick='searchFun(2,this);'>查询</a>" +
					"</div>" +
					"<p class='tool pad20' style='margin-top:0px' >" +
					"<a  href='javascript:void(0);' data-toggle='modal' class='btn btn-add-icon1 ' onclick='addOrModify(21,null)'><em></em>新建</a>" +
					"<a href='javascript:void(0);' class='btn-del-icon' onclick='deleteData(2,999,null);'><em></em>删除</a>" +
					"<a href='javascript:void(0);' class='btn-gb-icon' onclick='dataOnOrOff(2,999,0);'><em></em>停用</a>" +
					"<a href='javascript:void(0);' class='btn-qy-icon' onclick='dataOnOrOff(2,999,1);'><em></em>启用</a>" +
					"</p>" +
				"</div>";
	}else if (type==3){
		url=p_path+"/settings/prvlg/role/getRolesList.json";
		html += "<div class=\"set-t0 \">";
		html += "    <div class=\"se-bt clearFix\">";
		html += "        <form id='queryRolesByParam'><div class=\"sel w305\">";
		html += "            <i class=\"d-name\">角色名称<\/i>";
		html += "            <input name='ROLENAME' class=\"input-w295\" type=\"text\" placeholder=\"角色名称\">";
		html += "        <\/div>";
		html += "        <div class=\"sel w140\">";
		html += "            <i class=\"d-name\">角色编号<\/i>";
		html += "            <input name='ROLECODE' class=\"input-w140\" type=\"text\" placeholder=\"角色编号\">";
		html += "        <\/div></form>";
		html += "        <a onclick='searchByParam()' href=\"javascript:void(0)\" class=\"btn btn-w100 btn-search-icon btn-h30\" style=\"float:left\">查询<\/a>";
		html += "    <\/div>";
		html += "    <p class=\"tool pad20\" style=\"margin-top:0px\" >";
		html += "        <a onclick='clearAddRole()' href=\"#add-js\" data-toggle=\"modal\" class=\"btn btn-add-icon1 \"><em><\/em>新建<\/a>";
		html += "        <a onclick='delRole()' href=\"javascript:void(0)\" class=\"btn-del-icon\"><em><\/em>删除<\/a>";
		html += "        <a onclick='batchUpdateRoleStat(1)' href=\"javascript:void(0)\" class=\"btn-gb-icon\"><em><\/em>启用<\/a>";
		html += "        <a onclick='batchUpdateRoleStat(0)' href=\"javascript:void(0)\" class=\"btn-qy-icon\"><em><\/em>停用<\/a>";
		html += "    <\/p>";
		html += "    ";
		html += "<\/div>";
	}else if (type==4){
		url=p_path+"/settings/prvlg/menu/queryMenuList.json";
		html += "<div class=\"set-t0 \">";
		html += "    <div class=\"se-bt clearFix\">";
		html += "         <div class=\"sel w305\">";
		html += "             <i class=\"d-name\">菜单名<\/i>";
		html += "             <input id='queryMenuNameMan' class=\"input-w295\" type=\"text\" placeholder=\"\">";
		html += "         <\/div>";
		html += "         <a onclick='queryMenuListMan()' href=\"javascript:void(0)\" class=\"btn btn-w100 btn-search-icon btn-h30\" style=\"float:left\">查询<\/a>";
		html += "     <\/div>";
		html += "     <p class=\"tool pad20\" style=\"margin-top:0px\" >";
		//html += "         <a  href=\"#add-gncd\" data-toggle=\"modal\" class=\"btn btn-w80 btn-add-icon1 btn-h30\"><em><\/em>新建<\/a>";
		//html += "         <a href=\"\" class=\"btn-del-icon\"><em><\/em><\/a>";
		//html += "         <a href=\"\" class=\"btn-gb-icon\"><em><\/em><\/a>";
		//html += "         <a href=\"\" class=\"btn-qy-icon\"><em><\/em><\/a>";
		html += "     <\/p>";
		html += "   ";
		html += " <\/div>";
	}
		html += "<div id='dataTable'></div>";
		$(".set-ck").html(html);
		//列表加载
		$("#dataTable").initTable({
			url: url,
			columnData:dataList[type-1],
			selectDataNow:true,
			isBindDataToTr:true
		});

	initSelectUl();
}

/**
 * 表单操作
 * 
 */
var tableOpt = {
		getCheckBox:function(id){
			var hangClass = null == id ? "sw5-1" : "w5";
			var html = "<li style='list-style-type:none;' class='"+hangClass+"'><span class='table-ck'><span class='check-box selected' onclick=''><i id='"+id+"'></i>" +
				"<input name='ckInput_' type='hidden' value='"+id+"'/>" +
				"</span></span></li>";
			return html;
		},
		getState:function(type,data){
			var hangClass = "LOCKED" == data ? "on-off" : "on-off active";
			var html = "<li style='list-style-type:none;' class='s20-0-8'><em class='"+hangClass+"' onclick='changeState(\""+type+"\",this);'></em><input type='hidden' value='"+data+"'/></li>";
			return html;
		},
		getOpt:function(type,data){
			var html = "<li style='list-style-type:none;' class='w15 no-right'>" ;
			
				if(1==type){
					html += "<a href='javascript:void(0);' class='edit_00' onclick='addOrModify(12,this);'></a>"+
					"<a href='javascript:void(0);'  class='chongz_00' onclick='resetPwd(998,\""+data+"\")'></a>";
				}else if(2==type){
					html += "<a href='javascript:void(0);' class='edit_00' onclick='addOrModify(22,this);'></a>";
				}
				
				html += "<a href='javascript:void(0);'  class='delete_00' onclick='deleteData(\""+type+"\",998,\""+data+"\");'></a>" +
					"<a href='javascript:void(0);'  class='shouquan_00' onclick='authorizeFun(\""+type+"\",this);'></a>" +
				"</li>";
			return html;
		}
}

//数据列表数组
var dataList = [
                [{title:tableOpt.getCheckBox(null),name:"id",width:"5%",align:"center",format:function(json){return tableOpt.getCheckBox(json.id)}},        
					{title:"用户名称",name:"username",width:"10%",align:"center",showTips:true},
					{title:"姓名",name:"realName",width:"10%",align:"center",showTips:true},
					{title:"性别",name:"sex",width:"15%",align:"center",showTips:true},
					{title:"邮箱",name:"email",width:"10%",align:"center",showTips:true,format:function(json){return nvl_(json.email)}},
					{title:"手机号码",name:"mobile",width:"15%",align:"center",showTips:true,format:function(json){return nvl_(json.mobile)}},
					{title:"岗位",name:"position",width:"10%",align:"center",showTips:true},
					{title:"部门",name:"department",width:"10%",align:"center",showTips:true},
					{title:"公司",name:"company",width:"10%",align:"center",showTips:true},
					{title:"状态",name:"state",width:"10%",align:"center",format:function(json){ return tableOpt.getState(1,json.state)}},
					{title:"编辑  重置  删除  授权",name:"ID",width:"15%",align:"center",format:function(json){return tableOpt.getOpt(1,json.id)}},
					{title:"",name:"companyId",width:"",align:"center",format:function(json){return nvl_(json.companyId)}},
					{title:"",name:"departmentId",width:"",align:"center",format:function(json){return nvl_(json.departmentId)}},
					{title:"",name:"positionId",width:"",align:"center",format:function(json){return nvl_(json.positionId)}},
					{title:"",name:"empno",width:"",align:"center",format:function(json){return nvl_(json.empno)}},
					{title:"",name:"remark",width:"",align:"center",format:function(json){return nvl_(json.remark)}}
                 ],
                 [
                  {title:tableOpt.getCheckBox(null),name:"ID",width:"5%",align:"center",format:function(json){return tableOpt.getCheckBox(json.ID)}},        
                  {title:"用户组名称",name:"NAME",width:"10%",align:"center",showTips:true},
                  {title:"用户组编号",name:"CODE",width:"15%",align:"center",format:function(json){return nvl_(json.CODE)},showTips:true},
                  {title:"创建人",name:"CREATEUSER",width:"10%",align:"center"},
                  {title:"备注",name:"REMARK",width:"10%",align:"center",showTips:true,format:function(json){return nvl_(json.REMARK)}},
                  {title:"状态",name:"STATE",width:"10%",align:"center",format:function(json){ return tableOpt.getState(2,json.STATE)}},
                  {title:"编辑  删除  授权",name:"ID",width:"15%",align:"center",format:function(json){return tableOpt.getOpt(2,json.ID)}}
                  ],
	[
		{title:myTableOpt_.getCheckBox(null),name:"ID",width:"5%",align:"center",format:function(json){return myTableOpt_.getCheckBox(json.ID_)}},
		{title:"角色名称",name:"ROLENAME",width:"10%",align:"center",showTips:true},
		{title:"角色编号",name:"ROLECODE",width:"10%",align:"center",showTips:true},
		{title:"备注",name:"REMARK",width:"10%",align:"center",showTips:true,format:function(json){return nvl_(json.REMARK)}},
		{title:"状态",name:"STATE",width:"10%",align:"center",format:function(json){
			var stat_=json.STATE_==0?'active':'off';
			var funN="updateRoleStat('"+json.ID_+"','"+nvl_(json.STATE_)+"')";
			return myTableOpt_.getStatus(stat_,funN,"'"+json.ID_+"'");
		}},//STATE_ ID_
		{title:"编辑  删除  授权",name:"ID",width:"15%",align:"center",format:function(json){return roleTableOpt.getOpt(json)}}

	],
[
	{title:"菜单",name:"showName",width:"20%",align:"center",showTips:true},
	{title:"菜单编号",name:"code",width:"10%",align:"left",showTips:true},
	{title:"路径",name:"url",width:"20%",align:"center",showTips:true},
	{title:"图片",name:"imgUrl",width:"20%",align:"center"},
	{title:"状态",name:"STATE",width:"10%",align:"center",format:function(json){
		var stat_=json.stat==0?'active':'off';
		//var funN="updateRoleStatqqqq('"+json.id+"','"+nvl_(json.stat)+"')";
		return myTableOpt_.getStatus('active','',"'"+json.id+"'");
	}},//STATE_ ID_
	{title:"编辑",name:"ID",width:"15%",align:"center",format:function(json){
		var html = "<li style='list-style-type:none;' class='w15 no-right'>" ;
		html += "<a href='javascript:void(0);' class='edit_00' onclick=modifyMenuDiag(this)></a>";
			"</li>";
		return html}
	}

]

               ]


/**
 * 查询
 * @param type
 * @param obj
 */
function searchFun(type,obj){
	var data = null;
	if(1==type){
		var username = $.trim($("#searchUsername").val());
		var companyId = $("#SCompDiv").children().attr("id") == "gs" ? "" : $("#SCompDiv").children().attr("id");
		var departmentId = $("#SDeptDiv").children().attr("id") == "bm" ? "" : $("#SDeptDiv").children().attr("id");
		var isAuthorize = $("#SIsAuthorizeDiv").children().attr("id");
		data = {"username":username,"companyId":companyId,"departmentId":departmentId,"isAuthorize":isAuthorize}
		if(1 == isAuthorize){
			$(obj).parent().next().children("a:eq(1)").hide();
		}else{
			$(obj).parent().next().children("a:eq(1)").show();
		}
	}else if(2==type){
		var name = $.trim($(obj).parent().find("input").eq(0).val());
		var code = $.trim($(obj).parent().find("input").eq(1).val());
		data = {"name":name,"code":code}
	}
	$('#dataTable').selectDataAfterSetParm(data);
}

/**
 * @Title: showDialog   
 * @Description: 新增或修改用户
 * @param: @param obj      
 * @return: void      
 * @throws
 */
function addOrModify(optType,obj){
	if(11 == optType && null == obj){
		$("#add_user").find(".PopupTitle").html("新建用户信息");
		$("#userId").val("");
		$("#authorizeUser").val("");
		initView("#add_user");
		$("#add_user").find(".open-off").children("input").val("LOCKED");
		$("#sexDiv").children().text("请选择性别");
		reslutLoad.findCompany(null,null,null);
		$("#add_user").modal("show");
	}else if(12 == optType && null != obj){
		$("#add_user").find(".PopupTitle").html("修改用户信息");
		var companyId = $(obj).parent().parent().next().html();
		var departmentId = $(obj).parent().parent().next().next().html();
		var positionId = $(obj).parent().parent().next().next().next().html();
		var state = $(obj).parent().parent().prev().find("input").val();
		if("LOCKED" != state){
			alert("该数据不允许修改");
		}else{
			$("#username").val($(obj).parents("tr").find("td:eq(1)").html());
			$("#realName").val($(obj).parents("tr").find("td:eq(2)").html());
			var sex = $(obj).parents("tr").find("td:eq(3)").html();
			if("男" == sex){
				$("#sexDiv").children().text("男");
			}else{
				$("#sexDiv").children().text("女");
			}
			$("#email").val($(obj).parents("tr").find("td:eq(4)").html());
			$("#moblie").val($(obj).parents("tr").find("td:eq(5)").html());
			$("#remark").val($(obj).parents("tr").find("td:eq(15)").html());
			$("#code").val($(obj).parents("tr").find("td:eq(14)").html());
			$("#add_user").find(".open-off").children("input").val(state);
			var userId = $(obj).parents("tr").find("td:eq(0)").find("i").attr("id");
			$("#userId").val(userId);
			reslutLoad.findCompany(companyId,departmentId,positionId);
			$("#add_user").modal("show");
		}
	}else if(21==optType){
		$("#add_userGroup").find(".PopupTitle").html("新建用户组信息");
		$("#groupName").val("");
		$("#groupCode").val("");
		$("#groupRemark").val("");
		$("#groupId").val("");
		$("#add_userGroup").modal("show");
	}else if(22 == optType){
		$("#add_userGroup").find(".PopupTitle").html("修改用户组信息");
		var userGroupId = $(obj).parents("tr").find("td:eq(0)").find("i").attr("id");
		var name = $(obj).parents("tr").find("td:eq(1)").html();
		var code = $(obj).parents("tr").find("td:eq(2)").html();
		var remark = $(obj).parents("tr").find("td:eq(4)").html();
		$("#groupName").val(name);
		$("#groupCode").val(code);
		$("#groupRemark").val(remark);
		$("#groupId").val(userGroupId);
		$().invoke(pagePath + "/settings/userGroup/addOrModify.json",{"id":userGroupId},[function(m,r){
			var userGroup = r.userGroup;
			if("LOCKED" != userGroup.state){
				alert("该数据不允许修改");
			}else{
				$("#add_userGroup").modal("show");
			}
		}]);
	}
}


/**
 * 新增或修改页面的状态开关
 * @param obj
 */
function changeS(obj){
	$('.on-off').die();
	if($(obj).hasClass('active')){
		$(obj).removeClass('active');
		$(obj).next().val("LOCKED");
	}else{
		$(obj).addClass("active");
		$(obj).next().val("ACTIVATE");
	}
}

/**
 * 获取公司
 */
function getCompanyHtml(){
	var companyHtml = "<ul class='select select-min none'>";
	companyHtml += "<li onclick='getDepartment(this);'><a href='javascript:void(0);' id='gs'>请选择公司</a></li>";
	var url = pagePath + "/settings/user/ajaxGetCompany.json";
	$().invoke(url,{},[function(m,r){
		if(r.length > 0){
			$.each(r,function(i,evalue){
				companyHtml += "<li onclick='getDepartment(this);'><a href='javascript:void(0);' id='"+nvl_(evalue.id)+"'>"+nvl_(evalue.name)+"</a></li>";
			});
		}
	}],{async:false});
	companyHtml += "</ul>";
	return companyHtml;
}

/**
 * 初始化新增弹出框
 */
function initView(obj){
	var obj = $(obj);
	obj.find("input").val("");
	obj.find("textarea").val("");
	obj.find(".open-off").children("em").removeClass();
	obj.find(".open-off").children("em").addClass("on-off");
	$("#sexDiv").children().text("性别");
	$("#companyDiv").children().text("请选择公司");
	$("#departmentDiv").children().text("请选择部门");
	$("#positionDiv").children().text("请选择岗位");
}

function exportData(obj){
	var username = $.trim($("#searchUsername").val());
	var company = $("#SCompDiv").children().attr("id") == "gs" ? "" : $("#SCompDiv").children().attr("id");
	var department = $("#SDeptDiv").children().attr("id") == "bm" ? "" : $("#SDeptDiv").children().attr("id");
	$("input[name='username']").val(username);
	$("input[name='company']").val(company);
	$("input[name='department']").val(department);
	$("#exportForm").submit();
}


/**
 * 保存新增或修改内容
 * @param type[1:用户管理，2：用户组管理]
 * @returns {Boolean}
 */
function save(type){
	if(1==type){
		var regMobile = /^1[53874]\d{9}$/;
		var regEmail = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
		
		//用户名称
		var username = $.trim($("#username").val());
		//手机号码
		var mobile = $.trim($("#moblie").val());
		//邮箱
		var email = $.trim($("#email").val());
		//姓名
		var realName = $.trim($("#realName").val());
		//性别
		var sex = $("#sexDiv").children().text();
		//工号
		var code = $.trim($("#code").val());
		//公司
		var companyId = $("#companySelect").val();
		//部门
		var departmentId = $("#departmentSelect").val();
		//岗位
		var positionId = $("#positionSelect").val();
		//状态
		var state = $(".zhuangtai").children(".open-off").children("input").val();
		//备注
		var remark = $.trim($("#remark").val());
		//用户id
		var userId = $("#userId").val();
		
		if("" == username ){
			alert("请输入用户名称");
			return false;
		}else if("" != mobile && !mobile.match(regMobile)){
			alert("请输入有效的手机号码");
			return false;
		}else if("" != email && !email.match(regEmail)){
			alert("请输入有效的邮箱地址");
			return false;
		}else if("" == realName ){
			alert("请输入姓名");
			return false;
		}else if("男" != sex && "女" != sex){
			alert("请选择性别");
			return false;
		}/*else if("" == code ){
			alert("请输入工号");
			return false;
		}*/else
		if("" == companyId || undefined == companyId){
			alert("请选择公司");
			return false;
		}else
		if("" == departmentId || undefined == departmentId){
			alert("请选择部门");
			return false;
		}/*else
		if("" == positionId || undefined == positionId){
			alert("请选择岗位");
			return false;
		}*/else
		if("" == state || undefined == state){
			alert("请设置用户状态");
			return false;
		}else{
			var data = {"username":username,"mobile":mobile,"email":email,"realName":realName,"sex":sex,"code":code,"id":userId,
					"companyId":companyId,"departmentId":departmentId,"positionId":positionId,"state":state,"remark":remark}
			$().invoke(pagePath + "/settings/user/saveUser.json",data,[function(m,r){
				if(1==r){
					$("#add_user").modal("hide");
					$("#dataTable").refresh();
					alert("保存成功");
				}
			}]);
		}
	}else if(2==type){
		var groupId = $("#groupId").val();
		//组名
		var name = $.trim($("#groupName").val());
		//编码
		var code = $.trim($("#groupCode").val());
		
		var remark = $.trim($("#groupRemark").val());
		//状态
		var state = "LOCKED";
		var vR=$("#addUserGroup").validationEngine("validate");
		if (!vR){
			return;
		}
		if(""== name||name==null){
			alert("请输入用户组名称");
			return;
		}if(""== code||code==null){
			alert("请输入用户组编号");
			return;
		}else{
			var data = {"name":name,"code":code,"id":groupId,"state":state,"remark":remark};
			$().invoke(pagePath + "/settings/userGroup/saveGroup.json",data,[function(m,r){
				if(1==r){
					$("#add_userGroup").modal("hide");
					$("#dataTable").refresh();
					alert("操作成功");
				}
			}]);
		}
	}
}


/**
 * 
 * @Title: getDepartment   
 * @Description: 根据公司获取部门   
 * @param: @param obj      
 * @return: void      
 * @throws
 */
function getDepartment(obj){
	var companyId = $(obj).children().attr("id");
	var html = "";
	var dept = $("#SDeptDiv").parent().next();
	dept.html("");
	$().invoke(pagePath +"/settings/user/ajaxGetDepartment.json",{"companyId":companyId},[function(m,r){
		var list = r;
		if(list.length > 0){
			$("#SDeptDiv").html("<a href='javascript:void(0);' id='bm'>请选择部门</a>");
			html += "<li><a href='javascript:void(0);' id='bm'>请选择部门</a></li>"
			$.each(list,function(i,evalue){
				html += "<li><a href='javascript:void(0);' id='"+evalue.id+"'>"+evalue.name+"</a></li>";
			});
		}else{
			$("#SDeptDiv").html("<a href='javascript:void(0);' id='bm'>请选择部门</a>");
		}
		dept.html(html);
	}]);
}

/**
 * 点击列表状态栏，改变用户状态 
 * @param type [1:用户管理,2:用户组管理]
 * @param obj 
 */
function changeState(type,obj){
	$('.on-off').die();
	var dataId = $(obj).parents("tr").find("td:eq(0)").find("i").attr("id");
	var boolean = $(obj).hasClass("active");
	var state = "";
	if("" == dataId || undefined == dataId){
		alert("数据异常，不允许的操作");
		return;
	}
	if(!boolean){
		state = "ACTIVATE";
	}else{
		state = "LOCKED";
	}
	var url = "";
	if(1==type){
		url = pagePath + "/settings/user/changeState.json";
	}else if(2==type){
		url = pagePath + "/settings/userGroup/changeState.json";
	}
	window.cobj=obj;
	$().invoke(url,{"id":dataId,"state":state},[function(m,r){
		if(1== r){
			$(window.cobj).parent().find("input").val(state);
			if(!$(window.cobj).hasClass("active")){
				$(window.cobj).addClass("active");
			}else{
				$(window.cobj).removeClass("active");
			}
//			alert("操作成功");
		}
	}]);
}

/**
 * 删除数据
 * @param type 模块类型[1:用户管理,2:用户组管理]
 * @param commType 公用类型[999:多项操作,998:单项操作]
 * @param obj
 */
function deleteData(type,commType,obj){
	var idsArr = new Array();
	var idStr = "";
	var count = 0;
	var childBox = $("td .table-ck .check-box i");
	for(var i= 0 ; i < childBox.length;i ++){
		if(childBox.eq(i).hasClass("active")){
			idsArr.push(childBox.eq(i).attr("id"));
			count++;
		}
	}
	if(999 == commType && idsArr.length < 1){
		alert("请先勾选需要删除的数据");
		return ;
	}
	if(count == 0){
		idStr = obj;
	}else{
		idStr = idsArr.join();
	}
	var url = "";
	if(1==type){
		url = pagePath + "/settings/user/deleteData.json";
	}else if(2==type){
		url = pagePath + "/settings/userGroup/deleteData.json";
	}
	var delFlag = window.confirm("确定要删除吗");
	if(delFlag == true){
		$().invoke(url,{"idStr":idStr},[function(m,r){
			if(count == 0){
				if(r == 0){
					alert("该记录已经不允许删除");
				}else{
					alert("删除成功");
					$("#dataTable").refresh();
				}
			}else{
				alert("已选择【"+count+"】条，成功删除【 "+r +" 】条");
				$("#dataTable").refresh();
			}
		}]);
	}
}

/**
 * 加载授权信息
 * @param type [1:用户管理,2:用户组管理]
 * @param obj
 */
function authorizeFun(type,obj){
	var sh=["#user_sh","#userGroup_sh"];
	var shObj = sh[type-1];
	$(shObj).find(".PopupTitle").html("授权信息");
	var dataId = $(obj).parents("tr").find("td:eq(0)").find("i").attr("id");
	if("" == dataId || undefined == dataId){
		alert("数据异常，不允许的操作");
		return;
	}
	if(1==type){
		$().invoke(pagePath + "/settings/user/userAuthorize.json",{"id":dataId},[function(m,r){
			$("#authorizeUser").val(dataId);
			$("#user_sh").find(".li1").html("<span class='s1'> "+$(obj).parents("tr").find("td:eq(1)").html() +"("+$(obj).parents("tr").find("td:eq(2)").html()+")"+"</span>");
			var left = r.ug;
			var leftObj = $("#user_sh .left-ch").children("ul");
			var rightObj = $("#user_sh .right-ch").children("ul");
			var leftHtml ="";
			var rightHtml = "";
			var userGroup = r.userGroup;
			for (var i = 0; i < left.length; i++) {
				if(userGroup.indexOf(left[i].ID) != -1){
					rightHtml += "<li><input type='hidden' value='"+left[i].ID+"'/><span>"+left[i].NAME+"</span></li>";
				}else{
					leftHtml+= "<li><input type='hidden' value='"+left[i].ID+"'/><span>"+left[i].NAME+"</span></li>";
				}
			}
			leftObj.html(leftHtml);
			rightObj.html(rightHtml);
			authorize();
			$("#user_sh").modal("show");
		}]);
	}else if(2==type){
		$().invoke(pagePath + "/settings/userGroup/groupAuthorize.json",{"id":dataId},[function(m,r){
			$("#authorizeGroupUser").val(dataId);
			$("#userGroup_sh").find(".s1").html($(obj).parents("tr").find("td:eq(1)").html())
			var left = r.allRole;
			var leftObj = $("#userGroup_sh").find(".left-ch").children("ul");
			var rightObj = $("#userGroup_sh").find(".right-ch").children("ul");
			var leftHtml ="";
			var rightHtml = "";
			var role = r.role;
			for (var i = 0; i < left.length; i++) {
				if(role.indexOf(left[i].ID) != -1){
					rightHtml += "<li><input type='hidden' value='"+left[i].ID+"'/><span>"+left[i].NAME+"</span></li>";
				}else{
					leftHtml+= "<li><input type='hidden' value='"+left[i].ID+"'/><span>"+left[i].NAME+"</span></li>";
				}
			}
			leftObj.html(leftHtml);
			rightObj.html(rightHtml);
			authorize();
			$("#userGroup_sh").modal("show");
		}]);
	}
}


function authorizeMutiply(){
	var count = 0;
	var userIdArr = new Array();
	var html = "";
	var userObj = $("#user_sh").find(".li1");
	var childBox = $("td .table-ck .check-box i");
	$("#authorizeUser").val("");
	userObj.html("");
	for(var i= 0 ; i < childBox.length;i ++){
		if(childBox.eq(i).hasClass("active")){
			userIdArr.push(childBox.eq(i).attr("id"));
			html += "<span class='s1' style='margin-right:10px;width:auto;'>" +
				childBox.eq(i).parents("tr").find("td:eq(1)").html() +"("+childBox.eq(i).parents("tr").find("td:eq(2)").html() +")"+
					"</span><input type='hidden' value='"+childBox.eq(i).attr("id")+"' />";
			count++;
		}
	}
	if(userIdArr.length < 1){
		alert("请先勾选需要操作的数据");
		return ;
	}
	var idStr = userIdArr.join();
	$().invoke(pagePath + "/settings/user/userAuthorize.json",{"id":idStr},[function(m,r){
		var left = r.ug;
		var leftObj = $("#user_sh").find(".left-ch").children("ul");
		$("#user_sh").find(".right-ch").children("ul").html("");
		var htmlLi = "";
		for (var i = 0; i < left.length; i++) {
			htmlLi += "<li><input type='hidden' value='"+left[i].ID+"'/><span>"+left[i].NAME+"</span></li>";
		}
		leftObj.html(htmlLi);
		userObj.html(html);
		authorize();
		$("#user_sh").modal("show");
	}]);

}

/**
 * @Title: authorize   
 * @Description: 授权js操作
 * @param:       
 * @return: void      
 * @throws
 */
function authorize(){
	 $('.right-ch li').on('click',function(){
         $(this).addClass('active');
     })
     $('.left-ch li').on('click',function(){
         $(this).addClass('active');
     })
     $('.left-icon').on('click',function(){

         $('.right-ch ul ').append($('.left-ch ul li.active'));
         $('.right-ch ul li.active').removeClass("active");
     })
     $('.right-icon').on('click',function(){
         $('.left-ch ul ').append($('.right-ch ul li.active'));
         $('.left-ch ul li.active').removeClass("active");
     })
}

/**
 * 
 * @Title: saveAuthorize   
 * @Description: 保存授权   
 * @param:       
 * @return: void      
 * @throws
 */
function saveAuthorize(type){
	var sh=["#user_sh","#userGroup_sh"];
	var shObj = sh[type-1];
	var dataList = $(shObj).find(".right-ch").children("ul").children();
	var str = "";
	var dataArr = new Array();
	for (var i = 0; i < dataList.length; i++) {
		var data = $(shObj).find(".right-ch").children("ul").children("li").children("input").eq(i).val();
		dataArr.push(data);
	}
	str = dataArr.join();
	var url = "";
	var dataId = "";
	if(1== type){
		dataId = nvl_($("#authorizeUser").val());
		if("" == dataId){
			var userIdArr = new Array();
			var idObj = $(shObj).find(".li1").children("input");
			for (var int = 0; int < idObj.length; int++) {
				userIdArr.push(idObj.eq(int).val());
			}
			dataId = userIdArr.join();
		}
		url = pagePath + "/settings/user/saveAuthorize.json";
	}else if(2==type){
		dataId = $("#authorizeGroupUser").val();
		url = pagePath + "/settings/userGroup/saveAuthorize.json";
	}
	$().invoke(url,{"id":dataId,"str":str},[function(m,r){
		if(r > 0){
			$(shObj).modal("hide");
			$("#dataTable").refresh();
			alert("操作成功");
		}
	}]);
}



/**
 * 重置密码
 * @param obj
 */
function resetPwd(commType,obj){
	var idsArr = new Array();
	var count = 0;
	var childBox = $("td .table-ck .check-box i");
	for(var i= 0 ; i < childBox.length;i ++){
		if(childBox.eq(i).hasClass("active")){
			idsArr.push(childBox.eq(i).attr("id"));
			count++;
		}
	}
	if(999 == commType && idsArr.length < 1){
		alert("请先勾选需要操作的数据");
		return ;
	}
	if(count == 0){
		idStr = obj;
	}else{
		idStr = idsArr.join();
	}
	var flag = window.confirm("确定要执行该操作吗");
	if(flag == true){
		$().invoke(pagePath + "/settings/user/resetPwd.json",{"idStr":idStr},[function(m,r){
			if(999 == commType){
				alert("已选择【"+count+"】条，成功为【 "+r+" 】位用户重置密码");
			}else{
				alert("操作成功");
			}
			$("#dataTable").refresh();
		}]);
	}
}

/**
 * 启用或禁用
 * @param type 模块类型[1:用户管理,2:用户组管理]
 * @param commType 公用类型[999:多项操作,998:单项操作]
 * @param obj
 */
function dataOnOrOff(type,commType,obj){
	var idsArr = new Array();
	var count = 0;
	var childBox = $("td .table-ck .check-box i");
	for(var i= 0 ; i < childBox.length;i ++){
		if(childBox.eq(i).hasClass("active")){
			idsArr.push(childBox.eq(i).attr("id"));
			count++;
		}
	}
	if(999 == commType && idsArr.length < 1){
		alert("请先勾选需要操作的数据");
		return ;
	}
	var idStr = idsArr.join();
	var url = "";
	if(1==type){
		url = pagePath + "/settings/user/dataOnOrOff.json";
		if(0==obj){
			obj = "off";
		}else{
			obj = "on";
		}
	}else if(2==type){
		url = pagePath + "/settings/userGroup/dataOnOrOff.json";
		if(0==obj){
			obj = "off";
		}else{
			obj = "on";
		}
	}
	var flag = window.confirm("确定要执行该操作吗");
	if(flag == true){
		$().invoke(url,{"idStr":idStr,"opt":obj},[function(m,r){
			if("on" == obj){
				alert("已选择【"+count+"】条，成功启用【 "+r +" 】条");
			}else{
				alert("已选择【"+count+"】条，成功停用【 "+r +" 】条");
			}
			$("#dataTable").refresh();
		}]);
	}
}


function downloadModel(){
	window.location.href = pagePath + "/public/down/downExcelUserTemplate.json";
}

function importData(){
	var upPath = "/settings/user/importUser.json";
	var title = "用户导入";
	var data_={"upUrl":upPath,"title":title};
    openUploadDialog(data_);
}

/* 日期格式化 */
Date.prototype.Format = function(fmt) {
    var o = {
        "M+" : this.getMonth() + 1, //月份
        "d+" : this.getDate(), //日
        "h+" : this.getHours(), //小时
        "m+" : this.getMinutes(), //分
        "s+" : this.getSeconds(), //秒
        "q+" : Math.floor((this.getMonth() + 3) / 3), //季度
        "S" : this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
                .substr(4 - RegExp.$1.length));
    for ( var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1,
                    (RegExp.$1.length == 1) ? (o[k])
                            : (("00" + o[k])
                            .substr(("" + o[k]).length)));
    return fmt;
}





/************         公司-部门-岗位 联动开始          **********/
var reslutLoad = {
		findCompany:function(companyId,departmentId,positionId){
			$().invoke(pagePath + "/settings/user/ajaxGetCompany.json",{},[function(m,r){
				companyFun(m,r,companyId,departmentId,positionId);
			}]);
		},
		findDept:function(departmentId,positionId){
			var companyId = $("#companySelect").val();
			$().invoke(pagePath + "/settings/user/ajaxGetDepartment.json",{"companyId":companyId},[function(m,r){
				departmentFun(m,r,departmentId,positionId);
			}]);
		},
		findPosition:function(positionId){
			var departmentId = $("#departmentSelect").val();
			$().invoke(pagePath + "/settings/user/ajaxGetPosition.json",{"departmentId":departmentId},[function(m,r){
				positionFun(m,r,positionId);
			}]);
		},
		clearPosition:function(){
			$("#position").parent().parent().next("ul").find("li").remove();
		}
}

function companyFun(m,r,companyId,departmentId,positionId){
	var company =  $("#company").parent().parent().next("ul");
	if(null != r){
		company.html("");
		for (var i = 0; i < r.length; i++) {
			var li = "<li onclick='companyChange(this);'><input type='hidden' value='"+r[i].id+"'/><a href='javascript:void(0);'>"+r[i].name+"</a></li>";
			company.append(li);
		}
		if(companyId){
			var liList = company.children();
			for (var i = 0; i < liList.length; i++) {
				var selVal = $(liList[i]).children("input").val();
				if(selVal == companyId){
					$("#company").text($(liList[i]).children("a").text());
					$("#companySelect").val(selVal);
					break;
				}
			}
			reslutLoad.findDept(departmentId,positionId);
		}
	}
}

function departmentFun(m,r,departmentId,positionId){
	var department = $("#department").parent().parent().next("ul");
	if(null != r){
		department.html("");
		$("#department").text("请选择部门");
		$("#position").text("请选择岗位");
		
		for (var i = 0; i < r.length; i++) {
			var li = "<li onclick='departmentChange(this);'><input type='hidden' value='"+r[i].id+"'/><a href='javascript:void(0);' >"+r[i].name+"</a></li>";
			department.append(li);
		}
		if(departmentId){
			var liList = department.children();
			for (var i = 0; i < liList.length; i++) {
				var selVal = $(liList[i]).children("input").val();
				if(selVal == departmentId){
					$("#department").text($(liList[i]).children("a").text());
					$("#departmentSelect").val(selVal);
					break;
				}
			}
			reslutLoad.findPosition(positionId);
		}
	}
}

function positionFun(m,r,positionId){
	var position = $("#position").parent().parent().next("ul");
	if(null != r){
		position.html("");
		$("#position").text("请选择岗位");
		for (var i = 0; i < r.length; i++) {
			if(r[i]){
				var li = "<li onclick='positionChange(this)'><input type='hidden' value='"+r[i].id+"'/><a href='javascript:void(0);'>"+r[i].name+"</a></li>";
				position.append(li);
			}
		}
		if(positionId){
			var liList = position.children();
			for (var i = 0; i < liList.length; i++) {
				var selVal = $(liList[i]).children("input").val();
				if(selVal == positionId){
					$("#position").text($(liList[i]).children("a").text());
					$("#positionSelect").val(selVal);
					break;
				}
			}
		}
	}
}

function companyChange(obj){
	$("#companySelect").val(obj.firstChild.value);
	$(obj).children("a").attr("id","company");
	$("#departmentDiv").children().attr("id","department");
	$("#positionDiv").children().attr("id","position");
	$("#departmentSelect").val("");
	$("#positionSelect").val("");
	reslutLoad.findDept();
	reslutLoad.clearPosition();
}

function departmentChange(obj){
	$("#departmentSelect").val(obj.firstChild.value);
	$(obj).children("a").attr("id","department");
	$("#positionDiv").children().attr("id","position");
	$("#positionSelect").val("");
	reslutLoad.findPosition();
}

function positionChange(obj){
	$("#positionSelect").val(obj.firstChild.value);
	$(obj).children("a").attr("id","position");
}
/************         公司-部门-岗位 联动结束          **********/
